<?php

/* @version $Id: module_view_books.php,v 1.10 2008-02-13 20:30:22 oleg Exp $ */

/**
 * @see module_base
 */
class module_view_books extends module_base
{
    function get_book()
    {
        if (isset($_GET['id']))
        {
            $book = get_book($_GET['id']);
            return $book;
        }

        return new book();
    }

    function view()
    {
        global $WEB_APP;
        $WEB_APP['scripts'][] = 'jquery.js';
        $WEB_APP['scripts'][] = 'change_course.js';
        $WEB_APP['scripts'][] = 'get_book_description.js';

        $courses = get_unhidden_courses_by_login_password($_SESSION['user_login'], $_SESSION['user_password']);
        if (isset($_POST['course']))
        {
            $course = get_course($_POST['course']);
            $books = get_books_by_course_id_and_login_password($course->id, $_SESSION['user_login'], $_SESSION['user_password']);
            $course = $course->title;

        }
        else
        {
            $course = '';
            $books = array();
        }

        $book = new book();
        if (count($_POST) > 0)
        {
            $correct_post = TRUE;

            if (!isset($_POST['course']) || (isset($_POST['course']) && (trim($_POST['course']) == '')))
            {
                $WEB_APP['errorstext'] .= text('txt_insert_course').'<BR>';
                $books = array();
                $correct_post = FALSE;
            }
            if (!isset($_POST['book']) || (isset($_POST['book']) && (trim($_POST['book']) == '')))
            {
                $WEB_APP['errorstext'] .= text('txt_insert_book').'<BR>';
                $correct_post = FALSE;
            }

            if ($correct_post)
            {
                $book = get_book($_POST['book']);
                $_SESSION['book_id'] = $book->id;
                $WEB_APP['book'] = $book;
                $WEB_APP['id'] = $book->id;
                $WEB_APP['view']->display('view_book.tpl', text('txt_404_not_found'), TRUE);
                exit();
            }
        }

        $fields = array();
        $fields[] = new field(TRUE, text('txt_course'),
                    "select", "course",  $course, "",
                    $courses, "id", "title",
                    "return change_course()",
                    FALSE);
        $fields[] = new field(  TRUE, text('txt_book'), "select", "book",
                    $book->theme, "", $books, "id", "book_title",
                    "return get_book_description()", TRUE);

        $WEB_APP['fields'] = $fields;


        $WEB_APP['title'] = text('txt_view_books');
        $WEB_APP['form_title'] = text('txt_view_books');
        $WEB_APP['submit_title'] = text('txt_view');
        $WEB_APP['description_field'] = 'book_description';
        $WEB_APP['view']->display('form_page.tpl', text('txt_view_books'));
    }

    function menu()
    {
        global $WEB_APP;
        $book = get_book($_SESSION['book_id']);
        $WEB_APP['book'] = $book;
        $WEB_APP['view']->display('view_book_menu.tpl', text('txt_404_not_found'), TRUE);
    }

    function content()
    {
        global $WEB_APP;
        $book = get_book($_SESSION['book_id']);
        if (isset($_GET['id']))
        {
            $chapter = get_chapter_by_book_and_index($book->id, $_GET['id']);
        }
        else
        {
            $chapter = get_first_chapter_by_book_id($book->id);
        }
        $WEB_APP['book'] = $book;
        $WEB_APP['chapter'] = $chapter;
        $WEB_APP['view']->display('view_book_content.tpl', text('txt_404_not_found'), TRUE);
    }

    function show()
    {
        global $WEB_APP;
        if (isset($_GET['book_id']))
        {
            $book = get_book($_GET['book_id']);
            if (isset($book->id))
            {
                $books = get_books_by_login_password($_SESSION['user_login'], $_SESSION['user_password']);
                $b = FALSE;
                foreach($books as $tmp_book)
                {
                    $b = $b || ($book->id == $tmp_book['id']);
                }
                if ($b)
                {
                    $_SESSION['book_id'] = $book->id;
                    $WEB_APP['book'] = $book;
                    $WEB_APP['id'] = $book->id;
                    if (isset($_GET['chap_index']))
                    {
                        $chapter = get_chapter_by_book_and_index($book->id, $_GET['chap_index']);
                        $WEB_APP['book'] = $book;
                        $WEB_APP['chapter'] = $chapter;
                        $WEB_APP['view']->display('view_book_content.tpl', text('txt_404_not_found'), TRUE);
                        exit();
                    }
                    $WEB_APP['view']->display('view_book.tpl', text('txt_404_not_found'), TRUE);
                    exit();
                }

                $WEB_APP['action'] = 'view';
                $WEB_APP['title'] = text('txt_403_forbidden');
                $WEB_APP['view']->display('403.tpl', text('txt_403_forbidden'));
                exit();
            }
        }

        header('Location: '.$WEB_APP['cfg_url'].'?module=view_books');
    }

    
    function change_course()
    {
        global $WEB_APP;

        if (isset($_GET['course_id']) && is_scalar($_GET['course_id']))
        {
            $course_id = (int) $_GET['course_id'];
        }
        else
        {
            die();
        }
        
        $course = get_course($course_id);
        if (!isset($course->id)) 
        {
            die();
        }
        if ($course->hidden)
        {
            die();
        }

        $books = get_books_by_course_id($course->id);
        echo "<option selected=\"\" value=\"\"></option>\n";
        foreach($books as $book)
        {
            printf("<option value=\"%d\">%s</option>\n", $book['id'], htmlspecialchars($book['book_title']));
        }
        die();
    }

    function get_book_description()
    {
        global $WEB_APP;

        if (isset($_GET['book_id']) && is_scalar($_GET['book_id']))
        {
            $book_id = (int) $_GET['book_id'];
        }
        else
        {
            die();
        }
        
        $book = get_book($book_id);
        if (!isset($book->id)) 
        {
            die();
        }
        // Check is book enable for current user.
        $result = FALSE;
        $books = get_books_by_login_password($_SESSION['user_login'], $_SESSION['user_password']);
        foreach($books as $tmp)
        {
            $result = ($result || ($tmp['id'] == $book_id));
        }
        
        if ($result) 
        {
            echo $book->description;
        }
        die();
    }
}

